from starlette.endpoints import HTTPEndpoint
from starlette.responses import FileResponse, Response
from database import Session, engine
from dao import DaoPhotoIndex
from model.ModPhotoIndex import PhotoIndex as Photo
import config


class PhotoIndex(HTTPEndpoint):

    async def get(self, request):
        session = Session()
        try:
            photo_fingerprint = request.query_params["photo_fingerprint"]
            return  DaoPhotoIndex.get_photo_info(session, photo_fingerprint)
        finally:
            session.close()


class PhotoPreview(HTTPEndpoint):

    async def get(self, request):
        session = Session()
        try:
            fingerprint = request.path_params["fingerprint"]
            print(fingerprint)
            photo = session.query(Photo).filter(Photo.photo_fingerprint == fingerprint).one_or_none()
            if photo is None:
                return Response(status_code=404)
            return FileResponse(f"{config.UPLOAD_PATH}/{photo.access_path}")
        finally:
            session.close()
